@app.route('/ueditor', methods=['GET', 'POST'])
def ueditor():
    action = request.args.get("action")
    if action == 'config':
        with open("static/ueditor.config.json", 'r') as f:
            return Response(f.read(), mimetype='application/json')
    elif action == 'uploadimage':
        file = request.files['upfile']
        filename = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S') + "_" + file.filename
        file.save(os.path.join("static/upload", filename))
        return Response(json.dumps({
            "state": "SUCCESS",
            "url": filename,
            "title": filename,
            "original": filename
        }), mimetype='application/json')
    elif action == 'uploadvideo':
        file = request.files['upfile']
        filename = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S') + "_" + file.filename
        file.save(os.path.join("static/upload", filename))
        return Response(json.dumps({
            "state": "SUCCESS",
            "url": filename,
            "title": filename,
            "original": filename
        }), mimetype='application/json')
    elif action == 'uploadfile':
        file = request.files['upfile']
        filename = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S') + "_" + file.filename
        file.save(os.path.join("static/upload", filename))
        return Response(json.dumps({
            "state": "SUCCESS",
            "url": filename,
            "title": filename,
            "original": filename
        }), mimetype='application/json')
    elif action == 'uploadscrawl':
        data = request.form['upfile']
        img_data = base64.b64decode(data)
        filename = datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S') + "_" + 'scrawl.png'
        file = open(os.path.join("static/upload", filename), 'wb')
        file.write(img_data)
        file.close()
        return Response(json.dumps({
            "state": "SUCCESS",
            "url": filename,
            "title": filename,
            "original": filename
        }), mimetype='application/json')
    elif action == 'catchremoteimage':
        data_list = []
        sources = request.form.getlist("upfile[]")
        for src in sources:
            try:
                ext = os.path.splitext(src)[1]
                if not ext:
                    ext = ".png"
                filename = str(time.time()) + "_" + ext
                f = open(os.path.join("static/upload", filename), 'wb')
                f.write(urllib.request.urlopen(src).read())
                f.close()
                url = os.path.join("/static/upload/", filename)
            except Exception as e:
                print(e.args)
                url = src
            the_result = {"source": src, "url": url, "state": "SUCCESS"}
            data_list.append(the_result)
        return Response(json.dumps({"state": "SUCCESS", 'list': data_list}), mimetype='application/json')
